package com.ddstation.onsitelaw.controller;

import com.ddstation.onsitelaw.model.MaInvsBaseDrugModel;
import com.ddstation.onsitelaw.model.MaInvsDrugDetailModel;
import com.ddstation.onsitelaw.service.MaInvsBaseDrugService;
import com.ddstation.onsitelaw.utils.Constant;
import com.ddstation.onsitelaw.utils.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


/**
 * @author 陆青龙
 */
@Controller
public class DrugsController {

    private static final Logger logger = LoggerFactory.getLogger(DrugsController.class);

    @Inject
    private MaInvsBaseDrugService maInvsBaseDrugService;

    /**
     * 跳转药品查询页面
     */
    @RequestMapping(value = "drugsQuery")
    String drugsQuery(HttpSession session, Model model) {
        return "app/drugsQuery";
    }

    /**
     * 药品清单列表
     */
    @RequestMapping("drugs/drugList")
    @ResponseBody
    Map<String, Object> drugList(HttpSession session, @RequestParam("drugName") String drugName, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Constant.CODE_NAME, Constant.ERR_CODE);
        map.put(Constant.MSG_NAME, Constant.ERR_CODE_MSG);
        try {
            PageResult pageResult = maInvsBaseDrugService.queryDrugByName(drugName, pageNum, pageSize);
            map.put("maInvsBaseDrugList", pageResult.getRows());
            map.put("totalNum", pageResult.getTotal());
            //请求成功
            map.put(Constant.CODE_NAME, Constant.SUCCESS_CODE);
            map.put(Constant.MSG_NAME, Constant.SUCCESS_CODE_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("查询药品异常" + e.getMessage());
        }
        return map;
    }


    /**
     * 药品详情页
     */
    @RequestMapping("showDrugDetail")
    String showDrugDetail(Model model, @RequestParam Integer drugId) {
        MaInvsBaseDrugModel maInvsBaseDrug = maInvsBaseDrugService.showDrugDetail(drugId);
        model.addAttribute("maInvsBaseDrug", maInvsBaseDrug);
        return "app/drugDetail";
    }


    /**
     * 识别条形码
     */
    @RequestMapping("drugs/getDrugIdByBarCode")
    @ResponseBody
    Map<String, Object> getDrugIdByBarCode(@RequestParam String barCode) {

        Map<String, Object> map = new HashMap<>();
        map.put("errCode", 0);
        map.put("errMsg", "服务器繁忙，请稍后再试。");

        MaInvsDrugDetailModel maInvsDrugDetailModel = maInvsBaseDrugService.getMaInvsDrugDetail(barCode);
        if (ObjectUtils.isEmpty(maInvsDrugDetailModel)) {
            map.put("errMsg", "该条形码暂无相关药品信息！");
            return map;
        }

        map.put("errCode", 1);
        map.put("drugId", maInvsDrugDetailModel.getDrugId());
        map.put("errMsg", "成功");
        return map;
    }

}